﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace TSL_Tools.Utilities
{
    public class LogMsg
    {
        #region -- Properties ------------------------------------------------------------------------
        public static string sLastMessage { get; set; }

        public static LoggingLevel globalLevel = LoggingLevel.Summary;
        
        public static bool includeDateTime = true;

        public static string dateTimeFormatString = "MMM d, yyyy  h:mm:ss tt";

        public static bool writeToRtmon = false;
        #endregion

        #region -- Public Methods --------------------------------------------------------------------
        public static string WriteLine(string strMsg)
        {
            return Write(LoggingLevel.Summary, strMsg + "\r\n");
        }

        public static string Write(LoggingLevel level, string formatString, object arg1)
        {
            string strMsg = string.Format(formatString, arg1);
            return Write(level, strMsg);
        }

        public static string Write(LoggingLevel level, string formatString, object arg1, object arg2)
        {
            string strMsg = string.Format(formatString, arg1, arg2);
            return Write(level, strMsg);
        }

        public static string Write(LoggingLevel level, string formatString, params object[] args)
        {
            string strMsg = string.Format(formatString, args);
            return Write(level, strMsg);
        }

        public static string Write(string formatString, params object[] args)
        {
            string strMsg = string.Format(formatString, args);
            return Write(LoggingLevel.Debug, strMsg);
        }

        public static string Write(LoggingLevel level, string strMsg)
        {
            if (level > globalLevel)
                return string.Empty;

            string str;

            if (includeDateTime)
            {
                System.DateTime today = System.DateTime.Now;
                str = DateTime.Now.ToString(dateTimeFormatString, CultureInfo.CurrentCulture) + ": " + strMsg;
            }
            else
            {
                str = strMsg;
            }
            if (writeToRtmon)
                WriteTheMessage(level, str);
            else
                Console.Write(str);

            return str;
        }
        #endregion

        private static void WriteTheMessage(LoggingLevel level, string str)
        {

            //RTMonitor.SetHost("computerName", 11000, RTMonitor.ConnectionMethod.UDP);
            //RTMonitor.MsgLevel rtMonLevel = new RTMonitor.MsgLevel();
            //if (level == LoggingLevel.None) rtMonLevel = RTMonitor.MsgLevel.None;
            //if (level == LoggingLevel.Summary) rtMonLevel = RTMonitor.MsgLevel.Informational;
            //if (level == LoggingLevel.Error) rtMonLevel = RTMonitor.MsgLevel.Error;
            //if (level == LoggingLevel.Detailed) rtMonLevel = RTMonitor.MsgLevel.All;
            //if (level == LoggingLevel.Code) rtMonLevel = RTMonitor.MsgLevel.Warning;
            //if (level == LoggingLevel.Debug) rtMonLevel = RTMonitor.MsgLevel.Debug;

            //RTMonitor.Write(rtMonLevel, str);
        }
    }

    public enum LoggingLevel
    {
        /// <summary>
        /// Nothing is output
        /// </summary>
        None = 0,

        /// <summary>
        /// Output only errors
        /// </summary>
        Error = 1,

        /// <summary>
        /// Output errors and summary info
        /// </summary>
        Summary = 5,

        /// <summary>
        /// Output all messages except debug statements
        /// </summary>
        Detailed = 10,

        /// <summary>
        /// Used with Detailed messages to control the color of the code showing in RTMonitor
        /// </summary>
        Code = 12,

        /// <summary>
        /// Output all messages
        /// </summary>
        Debug = 15
    };
}
